#include "StaticMeshSceneProxy.h"

#include "MeshBatch.h"
#include "Runtime/Logic/Component/Primitive/StaticMeshComponent.h"

namespace Alice
{
    StaticMeshSceneProxy::StaticMeshSceneProxy(StaticMeshComponent* inStaticMeshComponent)
        :PrimitiveSceneProxy(inStaticMeshComponent)
    {
        bIsStaticMesh=true;
        mStaticMesh=inStaticMeshComponent->mStaticMesh;
        mStaticMeshResource=inStaticMeshComponent->mStaticMesh->GetRenderData();
        //mLODInfos.resize(1);//mStaticMeshResource->mStaticMeshLODResource)
        uint32 lodCount=1;//mStaticMeshResource->mStaticMeshLODResource;
        for(uint32 lodIndex=0;lodIndex<lodCount;lodIndex++)
        {
            FStaticMeshSection&staticMeshSection=mStaticMeshResource->mStaticMeshLODResource->mSections[lodIndex];
            LODInfo*lodInfo=new LODInfo;
            for(uint32 sectionIndex=0;sectionIndex<1;sectionIndex++)
            {
                LODInfo::SectionInfo* sectionInfo=new LODInfo::SectionInfo;
                sectionInfo->mMaterial=inStaticMeshComponent->mStaticMesh->mMaterials[staticMeshSection.MaterialIndex];
                lodInfo->mSectionInfos.push_back(sectionInfo);
            }
            mLODInfos.push_back(lodInfo);
        }
    }
    void StaticMeshSceneProxy::DrawStaticElements(FStaticPrimitiveDrawInterface* PDI)
    {
        int32 lodNum=1;
        int32 meshBatchCount=GetNumMeshBatches();
        PDI->ReserveMemoryForMeshes(meshBatchCount*(1));
        for(int32 meshBatchIndex=0;meshBatchIndex<meshBatchCount;meshBatchIndex++)
        {
            FMeshBatch baseMeshBatch;
            if(GetMeshElement(baseMeshBatch))
            {
                PDI->DrawMesh(baseMeshBatch,FLT_MAX);
            }
        }
        //Debug("StaticMeshSceneProxy::DrawStaticElements");
    }
    bool StaticMeshSceneProxy::GetMeshElement(FMeshBatch& OutMeshBatch) const
    {
	    const StaticMeshVertexFactories& VFs = mStaticMeshResource->mStaticMeshLODVertexFactories[0];
        uint32 primitiveCount=SetMeshElementGeometrySource(0,0,false,false,false,&VFs.mVertexFactory,OutMeshBatch);
        OutMeshBatch.LODIndex=0;
        return true;
    }
    uint32 StaticMeshSceneProxy::SetMeshElementGeometrySource(int32 LODIndex, int32 ElementIndex, bool bWireframe, bool bUseInversedIndices, bool bAllowPreCulledIndices,const VertexFactory* inVertexFactory, FMeshBatch& inOutMesh) const
    {
        inOutMesh.mVertexFactory=inVertexFactory;
        return 1;
    }
    SIZE_T StaticMeshSceneProxy::GetTypeHash() const
    {
        static SIZE_T sUniquePointer;
        return reinterpret_cast<SIZE_T>(&sUniquePointer);
    }

}
